@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: 记录开始时间
set "START_TIME=%TIME%"
echo 开始执行脚本: %START_TIME%

:: 定义 sh 目录路径
set "SH_DIR=C:\Users\A1846\Desktop\sh"

:: 获取当前目录
set "ROOT_DIR=%CD%"

:: 先处理当前目录
call :ProcessDirectory "%ROOT_DIR%"

:: 遍历当前目录及所有子目录
for /d /r "%ROOT_DIR%" %%D in (*) do call :ProcessDirectory "%%D"

:: 记录结束时间并计算执行时长
set "END_TIME=%TIME%"
echo 结束时间: %END_TIME%

:: 计算执行时长
for /f "tokens=1-4 delims=:.," %%a in ("%START_TIME%") do set /a "start=(((%%a*60)+%%b)*60+%%c)*100+%%d"
for /f "tokens=1-4 delims=:.," %%a in ("%END_TIME%") do set /a "end=(((%%a*60)+%%b)*60+%%c)*100+%%d"
set /a elapsed=end-start
set /a total_seconds=(elapsed/100)
echo 脚本执行完成，耗时: %total_seconds% 秒
exit /b

:ProcessDirectory
set "CURR_PATH=%~1"
set "CHECK_DIR=%~nx1"

:: 检查当前路径是否包含需要排除的目录
echo !CURR_PATH! | findstr /I /C:"\\classes\\" /C:"\\target\\" /C:"\\.idea\\" /C:"\\.myeclipse\\" /C:"\\.settings\\" /C:"\\DB\\" >nul
if !errorlevel! equ 0 exit /b

:: 删除特定的点文件
for %%F in (".classpath" ".mymetadata" ".project") do (
    if exist "!CURR_PATH!\%%~F" (
        echo [DELETE] %%~F
        del /F /Q "!CURR_PATH!\%%~F" >nul 2>&1
    )
)

:: 处理 src 目录
if /i "!CHECK_DIR!"=="src" (
    echo [INFO] 复制 Java 文件到: !CURR_PATH!
    for %%F in ("%SH_DIR%\*.java") do (
        echo [COPY] %%~nxF
    )
    xcopy /Y /Q "%SH_DIR%\*.java" "!CURR_PATH!\" >nul
)

:: 处理 lib 目录
if /i "!CHECK_DIR!"=="lib" (
    if exist "!CURR_PATH!" (
        echo !CURR_PATH! | findstr /I /C:"WEB-INF\lib" >nul
        if !errorlevel! equ 0 (
            echo [INFO] 处理 WEB-INF\lib 目录: !CURR_PATH!
            for %%F in ("!CURR_PATH!\mysql-connector*.jar") do (
                if exist "%%F" echo [DELETE] %%~nxF
            )
            del /F /Q "!CURR_PATH!\mysql-connector*.jar" 2>nul
            
            echo [INFO] 复制 JAR 文件到: !CURR_PATH!
            for %%F in ("%SH_DIR%\*.jar") do (
                echo [COPY] %%~nxF
            )
            xcopy /Y /Q "%SH_DIR%\*.jar" "!CURR_PATH!\" >nul
        )
    )
)
exit /b